#include <sstream>
#include <iostream>
#include <list>
#include <iterator>

using namespace std;

class Solution {
public:
    string retn;
    //比较相邻的2数字，后者比前者小，就删除前者
    string removeKdigits(string num, int k) {
        if(num.size() <= k) return "";
        list<char> res;
        int sz = num.size();
        for(int i = 0; i < sz; i++){
            res.push_back(num[i]);
        }
        print(res);
        list<char>::iterator it1 = res.begin();
        list<char>::iterator it2 = res.begin();
        advance(it2, 1);
        for (; it2 != res.end() && k > 0; it1++,it2++){
            cout<<"it1,it2="<<*it1<<","<<*it2<<endl;
            if(*it1 > *it2){
                res.erase(it1);
                k--;
            }
        }
        

        print(res);
        // 移除前导0
        while (*res.begin() == '0'){
            res.erase(res.begin());
        }
        std::ostringstream ret;
        for (list<char>::iterator it = res.begin(); it!= res.end();it++){
            ret<<*it;
        }
        // k个数还没拿完，递归再来一轮
        cout<<"k="<<k<<endl;
        cout<<"ret="<<ret.str()<<endl;
        if ( k > 0 ){
            retn = removeKdigits(ret.str(), --k);
        }
        retn = ret.str();
        return retn;

    }

    void print(list<char> input){
        cout << "list now: " << endl;
        for (auto i : input){
            cout << i <<" ";
        }
        cout << endl;
    }
};

int main(){
    Solution slu;

    string ret = slu.removeKdigits("1432219", 3);
    cout<<"result is: "<<slu.retn<<endl;
    return 0;
}